//
//  MMNewQianBaoVC.swift
//  movieInfo
//
//  Created by zhangkai on 2023/6/11.
//

import UIKit
import SwiftyStoreKit
import StatusAlert
class MMNewQianBaoVC: UIViewController {
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        self.navigationController?.setNavigationBarHidden(true, animated: true)
        
    }
    
    
    var curBtn = UIButton()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        
        

        
        
        
        
        

        // Do any additional setup after loading the view.
        let gradient = CAGradientLayer()
        gradient.startPoint = CGPointMake(0, 0);
        gradient.endPoint = CGPointMake(1, 0);
        gradient.frame = CGRectMake(0,0,UIScreen.main.bounds.size.width,500);
        gradient.colors = [UIColor(red: 1.0, green: 230/255.0, blue: 41/255.0, alpha: 1.0).cgColor, UIColor(red: 1.0, green: 230/255.0, blue: 41/255.0, alpha: 1.0).cgColor]
        self.view.layer.insertSublayer(gradient, at: 0)
        
        let icon = UIImageView()
        icon.image = UIImage(named: "vipbg")
        self.view.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.bottom.equalToSuperview()
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            if UIScreen.main.bounds.size.height > 800{
                make.top.equalToSuperview().offset(140)
            }else{
                make.top.equalToSuperview().offset(100)
            }
           
        }

        let vipIcon = UIImageView()
        vipIcon.image = UIImage(named: "vipicon")
        self.view.addSubview(vipIcon)
        vipIcon.snp.makeConstraints { make in
     
            make.top.equalTo(icon).offset(-70)
            make.centerX.equalToSuperview()
        }
        
        let oneView = UIView()
        oneView.backgroundColor = UIColor(red: 174/255.0, green: 92/255.0, blue: 0/255.0, alpha: 1.0)
        self.view.addSubview(oneView)
        oneView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(25)
            make.top.equalTo(icon.snp.top).offset(150)
            make.width.height.equalTo(4)
        }
        oneView.layer.cornerRadius = 2
        oneView.layer.masksToBounds = true
        
        let oneLabel = UILabel()
        oneLabel.text = "Daily collection quantity"
        oneLabel.textColor = UIColor(red: 177/255.0, green: 111/255.0, blue: 1/255.0, alpha: 1.0)
        oneLabel.font = UIFont.boldSystemFont(ofSize: 16)
        self.view.addSubview(oneLabel)
        oneLabel.snp.makeConstraints { make in
            make.left.equalTo(oneView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalTo(oneView)
        }
        
        let onesubLabel = UILabel()
        onesubLabel.numberOfLines = 0
        onesubLabel.text = "Increase the daily collection quantity from 3 to 10"
        onesubLabel.textColor = UIColor(red: 177/255.0, green: 111/255.0, blue: 1/255.0, alpha: 1.0)
        onesubLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        self.view.addSubview(onesubLabel)
        onesubLabel.snp.makeConstraints { make in
            make.left.equalTo(oneView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-5)
            make.top.equalTo(oneLabel.snp.bottom).offset(5)
        }
        
        
        let twoView = UIView()
        twoView.backgroundColor = UIColor(red: 174/255.0, green: 92/255.0, blue: 0/255.0, alpha: 1.0)
        self.view.addSubview(twoView)
        twoView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(25)
//            make.top.equalTo(120)
            make.top.equalTo(onesubLabel.snp.bottom).offset(20)
            make.width.height.equalTo(4)
        }
        twoView.layer.cornerRadius = 2
        twoView.layer.masksToBounds = true
        
        let twoLabel = UILabel()
        twoLabel.text = "Motivational advertising"
        twoLabel.textColor = UIColor(red: 177/255.0, green: 111/255.0, blue: 1/255.0, alpha: 1.0)
        twoLabel.font = UIFont.boldSystemFont(ofSize: 16)
        self.view.addSubview(twoLabel)
        twoLabel.snp.makeConstraints { make in
            make.left.equalTo(twoView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-5)
            make.centerY.equalTo(twoView)
        }
        
        let twosubLabel = UILabel()
        twosubLabel.numberOfLines = 0
        twosubLabel.text = "Activate VIP Exemption Advertising"
        twosubLabel.textColor = UIColor(red: 177/255.0, green: 111/255.0, blue: 1/255.0, alpha: 1.0)
        twosubLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
        self.view.addSubview(twosubLabel)
        twosubLabel.snp.makeConstraints { make in
            make.left.equalTo(twoView.snp.right).offset(5)
            make.right.equalToSuperview().offset(-5)
            make.top.equalTo(twoLabel.snp.bottom).offset(5)
        }
        
        
        let w = (UIScreen.main.bounds.size.width - 60 - 20) / 3
        for i in 0..<3{
            
            let btn = UIButton()
            btn.tag = i
            self.view.addSubview(btn)
            btn.setBackgroundImage(UIImage(named: "vipnosel"), for: .normal)
            btn.setBackgroundImage(UIImage(named: "vipsel"), for: .selected)

            btn.snp.makeConstraints { make in
                make.height.equalTo(130)
                make.width.equalTo(w)
                make.left.equalToSuperview().offset(30 + (w + 10.0) * CGFloat(i))
                make.top.equalTo(twosubLabel.snp.bottom).offset(30)
            }
            btn.addTarget(self, action: #selector(click(sender:)), for: .touchDown)
            
            
            let twoLabel = UILabel()
            twoLabel.numberOfLines = 0
      
           
            twoLabel.textAlignment = .center
            twoLabel.textColor = UIColor(red: 0/255.0, green: 0/255.0, blue: 0/255.0, alpha: 1.0)
            twoLabel.font = UIFont.boldSystemFont(ofSize: 20)
            btn.addSubview(twoLabel)
            twoLabel.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.top.equalToSuperview().offset(10)
            }
            
            let twosubLabel = UILabel()
            twosubLabel.numberOfLines = 0
           
            twosubLabel.textColor = UIColor(red: 169/255.0, green: 96/255.0, blue: 0/255.0, alpha: 1.0)
            twosubLabel.font = UIFont.systemFont(ofSize: 28, weight: .bold)
            btn.addSubview(twosubLabel)
            twosubLabel.snp.makeConstraints { make in

                make.centerX.equalToSuperview()
                make.bottom.equalToSuperview().offset(-15)
            }
            
            
//            每月4.99美元，每季度9.99美元，每年24.99美元
            
            if i == 0{
                twoLabel.text = "1\nMonth"
                twosubLabel.text = "$4.99"
            }else if i == 1{
                twoLabel.text = "3\nMonth"
                twosubLabel.text = "$9.99"
            }else{
                btn.isSelected = true
                self.curBtn = btn
                twoLabel.text = "12\nMonth"
                twosubLabel.text = "$24.99"
            }
            
        }
        
        
        
        
        let Subscription = UIButton()
        Subscription.setTitleColor(UIColor.white, for: .normal)
        Subscription.setTitle("Subscription", for: .normal)
        self.view.addSubview(Subscription)
        
        Subscription.backgroundColor = UIColor(red: 173/255.0, green: 111/255.0, blue: 1/255.0, alpha: 1.0)
        Subscription.snp.makeConstraints { make in
            make.height.equalTo(48)
            make.width.equalTo(300)
            make.centerX.equalToSuperview()
            make.top.equalTo(twosubLabel.snp.bottom).offset(200)
        }
        Subscription.addTarget(self, action: #selector(clickbuy), for: .touchDown)
        Subscription.layer.cornerRadius = 8
        Subscription.layer.masksToBounds = true
        
        
        let ResumePurchase = UIButton()
        ResumePurchase.setTitleColor(UIColor.white, for: .normal)
        ResumePurchase.setTitle("Resume Purchase", for: .normal)
        self.view.addSubview(ResumePurchase)
        ResumePurchase.backgroundColor = UIColor(red: 196/255.0, green: 150/255.0, blue: 80/255.0, alpha: 1.0)
        ResumePurchase.snp.makeConstraints { make in
            make.height.equalTo(48)
            make.width.equalTo(300)
            make.centerX.equalToSuperview()
            make.top.equalTo(Subscription.snp.bottom).offset(20)
        }
        ResumePurchase.addTarget(self, action: #selector(huifu), for: .touchDown)
        ResumePurchase.layer.cornerRadius = 8
        ResumePurchase.layer.masksToBounds = true
        
        
        
        let addImage = UIImage(systemName: "arrowshape.turn.up.backward.fill")
        let back = UIButton()
        back.setImage(addImage, for: .normal)
        self.view.addSubview(back)
        back.addTarget(self, action: #selector(dismisss), for: .touchDown)
        back.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(20)
            if UIScreen.main.bounds.size.height > 800{
                make.top.equalToSuperview().offset(68)
            }else{
                make.top.equalToSuperview().offset(40)
            }
        }

    }
    
    @objc func dismisss(){
        
        self.navigationController?.popViewController(animated: true)
    }
    
    @objc func click(sender:UIButton){
        
        self.curBtn.isSelected = false
        sender.isSelected = true
        self.curBtn = sender
        
        
        
        
        
//        FLIXLOGAAA1
        
        
       
        
        
 
        
        
    }
    
    
    @objc func clickbuy(){
        
        var str = ""
        if self.curBtn.tag == 0{
            str = "FLIXLOGAAA1"
        }else if self.curBtn.tag == 1{
            str = "FLIXLOGAAA3"
        }else if self.curBtn.tag == 2{
            str = "FLIXLOGAAA12"
        }else{
            return
        }
        
        
        
        SwiftyStoreKit.purchaseProduct(str, quantity: 1, atomically: true) { result in
            switch result {
            case .success(let purchase):
                print("Purchase Success: \(purchase.productId)")

                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "success"
                statusAlert.showInKeyWindow()
                
                UserDefaults.standard.set("1", forKey: "isVIP")
                UserDefaults.standard.synchronize()
        
               
            case .error(let error):
                
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "fail"
                statusAlert.showInKeyWindow()
                
                UserDefaults.standard.set("0", forKey: "isVIP")
                UserDefaults.standard.synchronize()
                
                
               
                print("Purchase Faile: \(error.code)")
                switch error.code {
                case .unknown: print("Unknown error. Please contact support")
                case .clientInvalid: print("Not allowed to make the payment")
                case .paymentCancelled: break
                case .paymentInvalid: print("The purchase identifier was invalid")
                case .paymentNotAllowed: print("The device is not allowed to make the payment")
                case .storeProductNotAvailable: print("The product is not available in the current storefront")
                case .cloudServicePermissionDenied: print("Access to cloud service information is not allowed")
                case .cloudServiceNetworkConnectionFailed: print("Could not connect to the network")
                case .cloudServiceRevoked: print("User has revoked permission to use this cloud service")
                case .privacyAcknowledgementRequired: break
                case .unauthorizedRequestData:break
                case .invalidOfferIdentifier:break
                case .invalidSignature:break
                case .missingOfferParams:break
                case .invalidOfferPrice:break
                case .overlayCancelled:break
                case .overlayInvalidConfiguration:break
                case .overlayTimeout:break
                case .ineligibleForOffer:break
                case .unsupportedPlatform:break
                case .overlayPresentedInBackgroundScene:break
                @unknown default:break

                }
            }
        }
        
        
    }
    

    
    @objc func huifu(){
        
        
        SwiftyStoreKit.restorePurchases(atomically: true, applicationUsername: "") { result in
            if result.restoreFailedPurchases.count > 0 {
                //恢复失败
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "Restore Failed"
                statusAlert.showInKeyWindow()
                

    
            }else if result.restoredPurchases.count > 0 {
                
                UserDefaults.standard.set("1", forKey: "isVIP")
                UserDefaults.standard.synchronize()
                
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "Restore Success"
                statusAlert.showInKeyWindow()
         
                
            }else{
                
         
                
                let statusAlert = StatusAlert()
                statusAlert.image = UIImage(named: "")
                statusAlert.title = ""
                statusAlert.message = "Nothing to Restore"
                statusAlert.showInKeyWindow()
    
               

            }
        }
        
        
        
        
    }

}
